package xc.example.libs

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.os.Environment
import android.os.FileObserver
import android.os.FileUtils
import com.example.libs.R
import xc.common.framework.net.NetClient
import xc.common.tool.Constents
import xc.common.tool.utils.*
import java.io.File
import java.lang.Exception
import kotlin.concurrent.thread

class MainActivity : AppCompatActivity() {

    val store = Environment.getExternalStorageDirectory().absolutePath + "/test_img"
    val file = File(store)
    val a: MyFileObserver by lazy {
        MyFileObserver(file.absolutePath)
    }


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        Constents.context = application

//        ft_basic

//        getExternalFilesDir()

        runOnUiThread {
            val findFragmentById = supportFragmentManager.findFragmentById(R.id.ft_basicinfo)
//            SWLog.e(findFragmentById)
        }

        LibPremissionUtils.needStore(this, object : PermissonSuccess {
            override fun hasSuccess() {
                SWLog.e("")

                if (!file.exists()) {
                    SWLog.e("创建文件夹 -》${file.mkdirs()}")
                }
                thread {

                    try {

                        while (true) {
                            var rootDir = file.absolutePath + "/"
                            Thread.sleep(2000)
                            File(rootDir + XcTimeUtils.get_yMd_hms1(System.currentTimeMillis()) + ".txt").createNewFile()
                        }
                    } catch (e: Exception) {
                        e.printStackTrace()
                    }
                }


                SWLog.e("store->${store}")

                a.startWatching()
            }

        }, object : PermissonFailure {
            override fun failure() {
                SWLog.e("")
            }

        })

    }


    class MyFileObserver(file: String) : FileObserver(file) {
        override fun onEvent(event: Int, path: String?) {
            SWLog.e("event->${event}          path->${path}")
        }

    }
}